﻿using UnityEngine;
using System.Collections;
using Game.Scripts.Managers;

namespace FrenzyIsland.UI
{
    public class UIMessageBoxDeleteBuilding : UIMessageBox
    {
        private bool requireRefresh = false;

        public Building building;
        public UIGrid gridResource;
        public UIAtlas iconAtlas;
        public UIFont fontResource;
        
        public override void Start()
        {
            requireRefresh = true;
            base.Start();
        }

        public void Update()
        {
            if( requireRefresh == true && building != null)
            {
                print("Load refound");

                int height = gridResource.transform.parent.gameObject.GetComponent<UIWidget>().height;

                Resource[] resources = building.prototype.GetListRefound((int)building.level);
                foreach (Resource res in resources)
                {
                    //creo la sprite
                    GameObject gamesprite = new GameObject("sprite");
                    UISprite uisprite = gamesprite.AddComponent<UISprite>();
                    uisprite.atlas = iconAtlas;
                    uisprite.spriteName = ResourceManager.instance.mappingIconResource[res.type];
                    uisprite.GetComponent<UIWidget>().height = height;
                    uisprite.GetComponent<UIWidget>().width = height;
                    uisprite.GetComponent<UIWidget>().depth = 5;
                    gamesprite.transform.parent = gridResource.transform;
                    gamesprite.transform.localPosition = Vector3.zero;
                    gamesprite.transform.localScale = Vector3.one;

                    //crea la label
                    GameObject objectlabel = new GameObject("label");
                    UILabel label = objectlabel.AddComponent<UILabel>();
                    label.text = res.amount.ToString();
                    label.bitmapFont = fontResource;
                    label.fontSize = 30;
                    label.GetComponent<UIWidget>().height = height;
                    label.GetComponent<UIWidget>().width = height;
                    label.GetComponent<UIWidget>().depth = 5;
                    objectlabel.transform.parent = gridResource.transform;
                    objectlabel.transform.localPosition = Vector3.zero;
                    objectlabel.transform.localScale = Vector3.one;

                }

                gridResource.Reposition();
                requireRefresh = false;
            }

        }

        public string Name {
            get
            {
                if (building == null) return "-";
                return building.prototype.name;
            }
        }

        public string IconName {
            get
            {
                if (building == null) return "Question-mark-icon";
                return building.prototype.iconName;
            }
        }

        public int Level
        {
            get
            {
                if (building == null) return 0;
                return (int)building.level;
            }
        }

    }
}